<template>
	<view>
		<cu-custom bgColor="bg-E96141" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">会员处方笺代录入</block>
		</cu-custom>
		<form>
			<view class="cu-form-group">
				<view class="title">账号</view>
				<input v-model="myFormData.memberUserAccount" disabled="disabled"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">电话</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.phone"
					maxlength="50"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">姓名</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.name"
					maxlength="50"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">性别</view>
				<switch class='switch-sex' 
					@change="SwitchC" 
					:class="switchC?'checked':''" 
					:checked="switchC?true:false"
					:color="'#E96141'"></switch>
			</view>
			<view class="cu-form-group">
				<view class="title">年龄</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.age"
					maxlength="3"></input>
			</view>
			
			<view class="cu-form-group margin-top">
				<view class="title">日期</view>
				<view class="uni-list-cell-db">
					<picker mode="date" :value="myFormData.date" :start="startDate" :end="endDate" 
						@change="bindDateChange">
						<view class="uni-input">{{myFormData.date?myFormData.date:'选择日期'}}</view>
					</picker>
				</view>
			</view>
			<!-- <view class="cu-form-group">
				<view class="title">发药</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.fayao"
					maxlength="3"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">费用</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.feey"
					maxlength="3"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">过敏史</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.guoms"
					maxlength="3"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">地址</view>
				<textarea placeholder="非必填，如：楼号、单元、楼层、门牌号等(100字以内)" 
					maxlength="100"
					:value="myFormData.liveAddress"
					:auto-size="{ minRows: 3, maxRows: 5 }"
					@input="addressDetailInput"
					style="border:1px solid #eeeeee; padding: 10upx;"/>
			</view> -->
			
			<view class="cu-form-group">
				<view class="title">诊断</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.zhend"
					maxlength="3"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">RP</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.rp"
					maxlength="3"></input>
			</view>
			
			<!-- <view class="cu-form-group">
				<view class="title">医师</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.doctor"
					maxlength="3"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">护士</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.nurse"
					maxlength="3"></input>
			</view> -->
			
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-blue lg" 
					style="background-color: #E96141;" 
					@click="onSubmit">保存</button>
			</view>
		</form>
	</view>
</template>

<script>
	
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				index: -1,
				switchC: true,
				imgList: [],
				uploadUrl:"/sys/common/upload",
				myFormData:{
					memberUserAccount: '',
					phone: '',
					name: '',
					sex: '',
					age: '',
					date: '',
					doctor: '',
					fayao: '',
					feey: '',
					guoms: '',
					liveAddress: '',
					nurse: '',
					rp: '',
					zhend: '',
				},
			};
		},
		onLoad: function (option) {
			this.myFormData.memberUserAccount = option.phone?option.phone:''
			this.myFormData.phone = option.phone?option.phone:''
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {
			//详情地址填写
			addressDetailInput(e){
				let value = e.detail.value
				if(!value || value.length<1){
					this.myFormData.detailAddress = ''
				}
				this.myFormData.detailAddress = value
			},
			//选择日期
			bindDateChange: function(e) {
				this.myFormData.date = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
	
				if (type === 'start') {
					year = year - 130;
				} else if (type === 'end') {
					year = year;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			//提交保存
			onSubmit() {
				uni.showLoading({
					title: '保存中..',
					mask: true,
				})
				let that = this
				that.$http.post("/chufangmanager/chufang/add",that.myFormData).then(res=>{
					if(res.data.success){
						uni.hideLoading()
						that.$tip.success('保存成功')
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							})
						}, 500);
					}else{
						uni.hideLoading()
						that.$tip.toast(res.data.message);	
					}
				}).catch(err => {
					uni.hideLoading()
					that.$tip.error('保存失败')
				});
			},
			/* DateChange(e) {
				this.myFormData.birthday = e.detail.value
			}, */
			SwitchC(e) {
				this.switchC = e.detail.value
			},
			ChooseImage() {
				var that=this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						that.$http.upload(that.$config.apiUrl+that.uploadUrl, {
								filePath: res.tempFilePaths[0],
								name: 'file'
							})
							.then(res => {
								that.myFormData.avatar=res.data.message;
							})
							.catch(err => {
								that.$tip.error(err.data.message)
							});
						this.imgList = res.tempFilePaths
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '召唤师',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
